package org.etherframework.tests.features.command
{
	import org.etherframework.common.command.ICommands;
	import org.etherframework.core.IModule;
	import org.etherframework.tests.features.command.commands.GetItemsCommand;
	import org.etherframework.tests.features.command.commands.SampleCommand;
	import org.etherframework.tests.features.command.events.GetItemsEvent;
	import org.etherframework.tests.utils.TestUtility;
	import org.hamcrest.assertThat;
	import org.hamcrest.collection.arrayWithSize;
	import org.hamcrest.object.notNullValue;

	public class when_removing_commands
	{
		private static var _testUtility:TestUtility = new TestUtility();
		private static var _module:IModule;
		private static var _c:ICommands;
		
		[BeforeClass]
		public static function setUpBeforeClass():void
		{
			_module = _testUtility.createModule();
			_c = _module.commands;
			_c.map(GetItemsEvent.GET_ITEMS, GetItemsCommand);
			_c.map(GetItemsEvent.GET_ITEMS, SampleCommand);
			_c.unmap(GetItemsEvent.GET_ITEMS, GetItemsCommand);
			_c.unmap(GetItemsEvent.GET_ITEMS, SampleCommand);
			
			GetItemsCommand.executed = new Vector.<GetItemsCommand>();
			SampleCommand.executed = new Vector.<SampleCommand>();
		}
		
		[AfterClass]
		public static function tearDownAfterClass():void
		{
			_testUtility.dispose();
		}
		
		[Test(order=1)]
		public function commands_should_be_removed():void
		{
			assertThat(_c.hasMapping(GetItemsEvent.GET_ITEMS), arrayWithSize(0));
		}
		
		[Test(order=2)]
		public function commands_should_not_be_executed_when_dispatching():void
		{
			_module.ether.dispatch(new GetItemsEvent(GetItemsEvent.GET_ITEMS));
			
			assertThat(GetItemsCommand.executed, arrayWithSize(0));
			assertThat(SampleCommand.executed, arrayWithSize(0));
		}
		
		[Test(order=3)]
		public function it_should_throw_an_error_when_arguments_are_not_valid():void
		{
			var err:Error;
			
			try {
				_c.unmap("", GetItemsCommand);
			} catch (e:*) {err = e;}
			assertThat(err, notNullValue());
			err = null;
			
			try {
				_c.unmap("", null);
			} catch (e:*) {err = e;}
			assertThat(err, notNullValue());
			err = null;
			
			try {
				_c.unmap(GetItemsEvent.GET_ITEMS, null);
			} catch (e:*) {err = e;}
			assertThat(err, notNullValue());
			err = null;
		}
		
		[Test(order=4)]
		public function it_should_throw_an_error_when_rule_type_is_already_removed():void
		{
			var err:Error;
			
			try {
				_c.unmap(GetItemsEvent.GET_ITEMS, GetItemsCommand);
			} catch (e:*) {err = e;}
			assertThat(err, notNullValue());
		}
	}
}